package com.ymatou.iapi.marketingmanage.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.ymatou.iapi.marketingmanage.parameter.CreateSellerPromotionBean;
import com.ymatou.iapi.marketingmanage.parameter.GetPromotionProductByIdBean;
import com.ymatou.iapi.marketingmanage.service.GetPromotionProductByIdCall;
import com.ymatou.iapi.trading.operate.parameter.enums.PromotionTypeEnum;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JSellerPromotionCallService;
import com.ymttest.database.mongo.YmtBuyerProductWapper;
/**
*查询促销商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetPromotionProductById{
	private static GetPromotionProductByIdBean getpromotionproductbyidBean;
	private static GetPromotionProductByIdCall getpromotionproductbyidCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询促销商品");
	}

	@Before
	public void caseUp() {
		getpromotionproductbyidBean=new GetPromotionProductByIdBean();
		getpromotionproductbyidCall=new GetPromotionProductByIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getpromotionproductbyidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_001() {
		Logger.start(true,"按sellerid和PromotionId查询指定促销商品成功");
		try {
			int sellerid=104;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getpromotionproductbyidBean.setPromotionId(pid);
			getpromotionproductbyidBean.setSellerId(sellerid);
			getpromotionproductbyidCall.setData(getpromotionproductbyidBean);
			getpromotionproductbyidCall.callService();			
			Logger.verifyEquals("操作成功", getpromotionproductbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionproductbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionproductbyidCall.getString("BCode"), "验证BCode");
			JsonArray productIds = getpromotionproductbyidCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("ProductIds");
			Logger.verifyEquals(1,productIds.size(),"验证productIds的size");
			List<String> pids = cspb.getPromotionInfo().getProductIds();
			for (String tpid : pids) {
				Logger.verifyIsContains(tpid,productIds.toString(),"验证productIds");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionProductById_002() {
		Logger.start(true,"不输入sellerid，失败");
		try {
			int sellerid=104;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getpromotionproductbyidBean.setPromotionId(pid);
//			getpromotionproductbyidBean.setSellerId(sellerid);
			getpromotionproductbyidCall.setData(getpromotionproductbyidBean);
			getpromotionproductbyidCall.callService();			
			Logger.verifyIsContains("无效的买手Id", getpromotionproductbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("400", getpromotionproductbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("-1", getpromotionproductbyidCall.getString("BCode"), "验证BCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionProductById_003() {
		Logger.start(true,"不输入PromotionId，失败");
		try {
			int sellerid=104;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
//			getpromotionproductbyidBean.setPromotionId(pid);
			getpromotionproductbyidBean.setSellerId(sellerid);
			getpromotionproductbyidCall.setData(getpromotionproductbyidBean);
			getpromotionproductbyidCall.callService();			
			Logger.verifyIsContains("无效的促销编号", getpromotionproductbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("400", getpromotionproductbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("-1", getpromotionproductbyidCall.getString("BCode"), "验证BCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionProductById_004() {
		Logger.start(true,"PromotionId和sellerid不匹配,失败");
		try {
			int sellerid=104;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getpromotionproductbyidBean.setPromotionId(pid);
			getpromotionproductbyidBean.setSellerId(sellerid+1);
			getpromotionproductbyidCall.setData(getpromotionproductbyidBean);
			getpromotionproductbyidCall.callService();			
			Logger.verifyEquals("操作成功", getpromotionproductbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionproductbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionproductbyidCall.getString("BCode"), "验证BCode");
			JsonArray productIds = getpromotionproductbyidCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("ProductIds");
			Logger.verifyEquals(0, productIds.size(), "验证ProductIds为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_005() {
		Logger.start(true,"按sellerid和PromotionId查询排除促销商品成功");
		try {
			int sellerid=104;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			Logger.debug(bpw);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			cspb.getPromotionInfo().getProductIds().add(bpw.get(1).get("ProductId").toString());
			cspb.getPromotionInfo().setIsLimitProduct(true);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getpromotionproductbyidBean.setPromotionId(pid);
			getpromotionproductbyidBean.setSellerId(sellerid);
			getpromotionproductbyidCall.setData(getpromotionproductbyidBean);
			getpromotionproductbyidCall.callService();			
			Logger.verifyEquals("操作成功", getpromotionproductbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionproductbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionproductbyidCall.getString("BCode"), "验证BCode");
			JsonArray productIds = getpromotionproductbyidCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("ProductIds");
			Logger.verifyEquals(2,productIds.size(),"验证productIds的size");
			List<String> pids = cspb.getPromotionInfo().getProductIds();
			for (String tpid : pids) {
				Logger.verifyIsContains(tpid,productIds.toString(),"验证productIds");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_006() {
		Logger.start(true,"按sellerid和PromotionId查询全场促销商品成功");
		try {
			int sellerid=104;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getpromotionproductbyidBean.setPromotionId(pid);
			getpromotionproductbyidBean.setSellerId(sellerid);
			getpromotionproductbyidCall.setData(getpromotionproductbyidBean);
			getpromotionproductbyidCall.callService();			
			Logger.verifyEquals("操作成功", getpromotionproductbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionproductbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionproductbyidCall.getString("BCode"), "验证BCode");
			JsonArray productIds = getpromotionproductbyidCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("ProductIds");
			//排除
			if(productIds!=null){
				Logger.verifyEquals(0,productIds.size(),"验证productIds的size");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}